using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    [Serializable]
    public class FileWorker:IWorker
    {
        private Thread _WorkerThread;
        private FileInstruction _Instruction;
        private FileOutput _Output;
        private Guid _TicketID;
        private RunContext _Context;
        private string _WorkflowName;
        private string _ComponentName;

        /// <summary>
        /// 
        /// </summary>
        public string ComponentName
        {
            get { return _ComponentName; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string WorkflowName
        {
            get { return _WorkflowName; }
        }

        public Thread WorkerThread
        {
            get { return _WorkerThread; }
        }

        public InstructionBase Instruction
        {
            get { return _Instruction; }
        }

        public ResultBase Output
        {
            get { return this._Output; }
        }

        public Type ComponentType
        {
            get { return typeof(FileComponent); }
        }

        public RunContext Context
        {
            get { return _Context; }
            set { _Context = value; }
        }

        public void SetInstruction(Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._TicketID = ticketGuid;
            this._Instruction = (FileInstruction) request;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }

        public void SetInstruction(RunContext context, Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._Context = context;
            this._TicketID = ticketGuid;
            this._Instruction = (FileInstruction) request;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }

        public void DoWork()
        {
            if(this.WorkerStartedEvent !=null)
            {
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this._Instruction);
            }
            this._WorkerThread = Thread.CurrentThread;
            this.ManipulateFile();
        }

        public void DoWorkAsync()
        {
            if (this.WorkerStartedEvent != null)
            {
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this._Instruction);
            }
            this._WorkerThread = new Thread(new ThreadStart(this.ManipulateFile));
            this._WorkerThread.Start();
        }

        public void CancelWork()
        {
            if(this._WorkerThread !=null && this._WorkerThread.IsAlive)
            {
                this._WorkerThread.Abort();
                if(this.WorkerCancelledEvent !=null)
                {
                    this.WorkerCancelledEvent(this._TicketID, this.GetType(), this._Instruction);
                }
            }
        }

        public event WorkerStarted WorkerStartedEvent;
        public event WorkerFinished WorkerFinishedEvent;
        public event WorkerHasError WorkerErredEvent;
        public event WorkerCancelled WorkerCancelledEvent;
        public event WorkerStatusChanged WorkerStatusChangedEvent;

        #region work
        private void ManipulateFile()
        {
            try
            {
                string tgtFilePath = this._Instruction.TgtPath;
                switch(this._Instruction.ActionType)
                {
                    case FileActionType.Move:
                        tgtFilePath =
                            Path.Combine(this._Instruction.TgtPath, Path.GetFileName(this._Instruction.SrcPath));
                        File.Move(this._Instruction.SrcPath, tgtFilePath);
                        break;
                    case FileActionType.Copy:
                        tgtFilePath =
                            Path.Combine(this._Instruction.TgtPath, Path.GetFileName(this._Instruction.SrcPath));
                        File.Copy(this._Instruction.SrcPath, tgtFilePath, true);
                        break;
                    case FileActionType.Delete:
                        File.Delete(this._Instruction.SrcPath);
                        tgtFilePath = string.Empty;
                        break;
                    case FileActionType.Rename:
                        tgtFilePath =
                            Path.Combine(Path.GetDirectoryName(this._Instruction.SrcPath), this._Instruction.TgtPath);
                        File.Copy(this._Instruction.SrcPath, tgtFilePath, true);
                        File.Delete(this._Instruction.SrcPath);
                        break;
                    default:
                        break;
                }
                this._Output = new FileOutput(this._Instruction.OwnerComponentID, this._TicketID, tgtFilePath);
                    
                if(this.WorkerFinishedEvent !=null)
                {
                    this.WorkerFinishedEvent(this._TicketID, this.GetType(), this._Instruction, this._Output);
                }
            }
            catch(Exception ex)
            {
                if(this.WorkerErredEvent !=null)
                {
                    this.WorkerErredEvent(this._TicketID, this.GetType(), this._Instruction, ex.ToString(),true);
                }
            }
        }
        #endregion
    }
}
